package com.dykj.fireFighting.web.base.analysis.controller;

import com.dykj.fireFighting.api.base.analysis.dto.ComprehensiveQueryDto;
import com.dykj.fireFighting.api.base.analysis.service.IFaultRecoveryRecordService;
import com.dykj.fireFighting.api.base.analysis.vo.ComprehensiveResultFLVo;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.DateTimeCutUtil;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: zly
 * @Description : 综合查询 复核率 处置率 故障恢复率
 * @Date: 2022-04-22
 */
@RestController
@RequestMapping("comprehensiveQuery")
public class ComprehensiveQueryFLController {

    @DubboReference
    private IDevHisResultService devHisResultService;
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private IFaultRecoveryRecordService faultRecoveryRecordService;
    @DubboReference
    private ISysDirectoryService iSysDirectoryService;


    @AutoLog(value = "综合查询-故障恢复趋势变化图")
    @ApiOperation(value = "综合查询-故障恢复趋势变化图", notes = "综合查询-故障恢复趋势变化图")
    @GetMapping("/getFaultRecoveryQS")
    public CommonResult getFaultRecoveryQS(ComprehensiveQueryDto queryDto) throws ParseException {
          // 数据维度 1单位 2区域 3行业
          List<ComprehensiveResultFLVo>  list=null;
          if("1".equals(queryDto.getDataDimension())){
              String unitIds = queryDto.getUnitIds();
              if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                  List<String> strings = Arrays.asList(unitIds.split(","));
                  queryDto.setUnitIdList(strings);
              }else{
                  LoginUserVo user = ShiroUtil.getUser();
                  List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                  List<String> collect = enterprises.stream().map(Enterprise -> {
                      return Enterprise.getId().toString();
                  }).collect(Collectors.toList());
                  queryDto.setUnitIdList(collect);
              }
              list=faultRecoveryRecordService.getFaultRecoveryQSByUnits(queryDto);
          }
          if("2".equals(queryDto.getDataDimension())){
              list=faultRecoveryRecordService.getFaultRecoveryQSByArea(queryDto);
          }
          if("3".equals(queryDto.getDataDimension())){
              list=faultRecoveryRecordService.getFaultRecoveryQSByIndustry(queryDto);
          }
        List<String> resDateNameList = list.stream().map(ComprehensiveResultFLVo -> {
            return ComprehensiveResultFLVo.getResDate() + "," + ComprehensiveResultFLVo.getResName();
        }).collect(Collectors.toList());
        // 数据补充零
        List<ComprehensiveResultFLVo> bulingList =devHisResultService.buling(queryDto,resDateNameList);

        list.addAll(bulingList);
        list = list.stream().sorted(Comparator.comparing(ComprehensiveResultFLVo::getResDate))
                .collect(Collectors.toList());
          return CommonResult.success(list);


    }


    @AutoLog(value = "综合查询-故障恢复排行榜")
    @ApiOperation(value = "综合查询-故障恢复排行榜", notes = "综合查询-故障恢复排行榜")
    @GetMapping("/getFaultRecoveryPh")
    public CommonResult getFaultRecoveryPh(ComprehensiveQueryDto queryDto){
        // 数据维度 1单位 2区域 3行业
        List<ComprehensiveResultFLVo>  list=null;
        if("1".equals(queryDto.getDataDimension())){
            String unitIds = queryDto.getUnitIds();
            if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                List<String> strings = Arrays.asList(unitIds.split(","));
                queryDto.setUnitIdList(strings);
            }else{
                LoginUserVo user = ShiroUtil.getUser();
                List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                List<String> collect = enterprises.stream().map(Enterprise -> {
                    return Enterprise.getId().toString();
                }).collect(Collectors.toList());
                queryDto.setUnitIdList(collect);
            }
            list=faultRecoveryRecordService.getFaultRecoveryPHByUnits(queryDto);
        }
        if("2".equals(queryDto.getDataDimension())){
            list=faultRecoveryRecordService.getFaultRecoveryPHByArea(queryDto);
        }
        if("3".equals(queryDto.getDataDimension())){
            list=faultRecoveryRecordService.getFaultRecoveryPHByIndustry(queryDto);

        }
        return CommonResult.success(list);


    }


    @AutoLog(value = "综合查询-复核率趋势变化图")
    @ApiOperation(value = "综合查询-复核率趋势变化图", notes = "综合查询-复核率趋势变化图")
    @GetMapping("/getFuHeLvQuShi")
    public CommonResult getFuHeLvQuShi(ComprehensiveQueryDto queryDto) throws ParseException {
            // 数据维度 1单位 2区域 3行业
        List<ComprehensiveResultFLVo>  list=null;
        if("1".equals(queryDto.getDataDimension())){
              String unitIds = queryDto.getUnitIds();
              if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                  List<String> strings = Arrays.asList(unitIds.split(","));
                  queryDto.setUnitIdList(strings);
              }else{
                  LoginUserVo user = ShiroUtil.getUser();
                  List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                  List<String> collect = enterprises.stream().map(Enterprise -> {
                      return Enterprise.getId().toString();
                  }).collect(Collectors.toList());
                  queryDto.setUnitIdList(collect);
              }
            list=devHisResultService.getFuHeLvQSByUnits(queryDto);
          }
          if("2".equals(queryDto.getDataDimension())){
              list=devHisResultService.getFuHeLvQSByArea(queryDto);
          }
          if("3".equals(queryDto.getDataDimension())){
              list=devHisResultService.getFuHeLvQSByIndustry(queryDto);

          }

        List<String> resDateNameList = list.stream().map(ComprehensiveResultFLVo -> {
            return ComprehensiveResultFLVo.getResDate() + "," + ComprehensiveResultFLVo.getResName();
        }).collect(Collectors.toList());
        // 数据补充零
        List<ComprehensiveResultFLVo> bulingList =devHisResultService.buling(queryDto,resDateNameList);

        list.addAll(bulingList);
        list = list.stream().sorted(Comparator.comparing(ComprehensiveResultFLVo::getResDate))
                .collect(Collectors.toList());

        return CommonResult.success(list);
    }

    @AutoLog(value = "综合查询-复核率排名榜")
    @ApiOperation(value = "综合查询-复核率排名榜", notes = "综合查询-复核率排名榜")
    @GetMapping("/getFuHeLvPaih")
    public CommonResult getFuHeLvPaih(ComprehensiveQueryDto queryDto){
        // 数据维度 1单位 2区域 3行业
        List<ComprehensiveResultFLVo>  list=null;
        if("1".equals(queryDto.getDataDimension())){
            String unitIds = queryDto.getUnitIds();
            if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                List<String> strings = Arrays.asList(unitIds.split(","));
                queryDto.setUnitIdList(strings);
            }else{
                LoginUserVo user = ShiroUtil.getUser();
                List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                List<String> collect = enterprises.stream().map(Enterprise -> {
                    return Enterprise.getId().toString();
                }).collect(Collectors.toList());
                queryDto.setUnitIdList(collect);
            }
            list=devHisResultService.getFuHeLvPHByUnits(queryDto);
        }
        if("2".equals(queryDto.getDataDimension())){
            list=devHisResultService.getFuHeLvPHByArea(queryDto);
        }
        if("3".equals(queryDto.getDataDimension())){
            list=devHisResultService.getFuHeLvPHByIndustry(queryDto);

        }
        return CommonResult.success(list);

    }

    @AutoLog(value = "综合查询-处置率趋势变化图")
    @ApiOperation(value = "综合查询-处置率趋势变化图", notes = "综合查询-处置率趋势变化图")
    @GetMapping("/getChuZhiLvQuShi")
    public CommonResult getChuZhiLvQuShi(ComprehensiveQueryDto queryDto) throws ParseException {
        // 数据维度 1单位 2区域 3行业
        List<ComprehensiveResultFLVo>  list=null;
        if("1".equals(queryDto.getDataDimension())){
            String unitIds = queryDto.getUnitIds();
            if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                List<String> strings = Arrays.asList(unitIds.split(","));
                queryDto.setUnitIdList(strings);
            }else{
                LoginUserVo user = ShiroUtil.getUser();
                List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                List<String> collect = enterprises.stream().map(Enterprise -> {
                    return Enterprise.getId().toString();
                }).collect(Collectors.toList());
                queryDto.setUnitIdList(collect);
            }
            list=devHisResultService.getChuZhiLvQSByUnits(queryDto);
        }
        if("2".equals(queryDto.getDataDimension())){
            list=devHisResultService.getChuZhiLvQSByArea(queryDto);
        }
        if("3".equals(queryDto.getDataDimension())){
            list=devHisResultService.getChuZhiLvQSByIndustry(queryDto);

        }

        List<String> resDateNameList = list.stream().map(ComprehensiveResultFLVo -> {
            return ComprehensiveResultFLVo.getResDate() + "," + ComprehensiveResultFLVo.getResName();
        }).collect(Collectors.toList());
        // 数据补充零
        List<ComprehensiveResultFLVo> bulingList =devHisResultService.buling(queryDto,resDateNameList);

        list.addAll(bulingList);
        list = list.stream().sorted(Comparator.comparing(ComprehensiveResultFLVo::getResDate))
                .collect(Collectors.toList());
        return CommonResult.success(list);
    }

    @AutoLog(value = "综合查询-处置率排行榜")
    @ApiOperation(value = "综合查询-处置率排行榜", notes = "综合查询-处置率排行榜")
    @GetMapping("/getChuZhiLvPaih")
    public CommonResult getChuZhiLvPaih(ComprehensiveQueryDto queryDto){
        // 数据维度 1单位 2区域 3行业
        List<ComprehensiveResultFLVo>  list=null;
        if("1".equals(queryDto.getDataDimension())){
            String unitIds = queryDto.getUnitIds();
            if(null != unitIds && StringUtils.isNotBlank(unitIds)){
                List<String> strings = Arrays.asList(unitIds.split(","));
                queryDto.setUnitIdList(strings);
            }else{
                LoginUserVo user = ShiroUtil.getUser();
                List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
                List<String> collect = enterprises.stream().map(Enterprise -> {
                    return Enterprise.getId().toString();
                }).collect(Collectors.toList());
                queryDto.setUnitIdList(collect);
            }
            list=devHisResultService.getChuZhiLvPHByUnits(queryDto);
        }
        if("2".equals(queryDto.getDataDimension())){
            list=devHisResultService.getChuZhiLvPHByArea(queryDto);
        }
        if("3".equals(queryDto.getDataDimension())){
            list=devHisResultService.getChuZhiLvPHByIndustry(queryDto);

        }
        return CommonResult.success(list);
    }

    /**
     * fault_recovery_record 表批量增加数据
     * @return
     */
    @AutoLog(value = "fault_recovery_record 增加")
    @ApiOperation(value = "fault_recovery_record增加", notes = "fault_recovery_record增加")
    @GetMapping("/batchSave")
    public CommonResult batchSave() {
        try {

            faultRecoveryRecordService.bsave();
            return CommonResult.success("添加成功");
        } catch (Exception e) {

            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

}
